﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class PermissionBussiness:IDbServices<bool, int, TblPermissionsForm>
    {
        BookingDB db = new BookingDB();

        public bool AddData(TblPermissionsForm e)
        {
            try
            {

                db.TblPermissionsForms.Add(e);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }


        }

        public bool UpdateData(TblPermissionsForm e)
        {
            try
            {

                db.Entry(e).State = EntityState.Modified;
                db.SaveChanges();
                return true;
            }
            catch (ValidationException ex)
            {

                return false;
            }
        }

        public bool DeleteData(int key)
        {
            TblPermissionsForm objRoomType = new TblPermissionsForm();
            objRoomType = db.TblPermissionsForms.Find(key);
            try
            {
                db.TblPermissionsForms.Remove(objRoomType);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }

        }

        public TblPermissionsForm GetDataByKey(int k)
        {
            try
            {
                return db.TblPermissionsForms.Find(k);
            }
            catch (Exception)
            {

                return null;
            }
        }

        public List<TblPermissionsForm> GetAll()
        {
            try
            {
                return db.TblPermissionsForms.ToList<TblPermissionsForm>();
            }
            catch (Exception)
            {

                return null;
            }
        }

        public List<TblPermissionsForm> GetAllDataByChucVu(int k)
        {
            var temp = db.TblPermissionsForms.Where(b => b.PositionsID == k);
            return temp.ToList<TblPermissionsForm>();
        }

        public bool DeleteDataByPositionID(int k)
        {
            try
            {
                db.Database.ExecuteSqlCommand("Delete from TblPermissionsForm where  PositionsID={0}", k);
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }



    }
}
